﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using Memcached;
using System.Configuration;
using System.IO;

public partial class gold_eGoldTjtxChart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strDate = System.DateTime.Now.Date.ToString("yyyyMMdd");
        //如果没有当天的文件夹，则生成    
        string strFilePath = ConfigurationManager.AppSettings["eGold_filepath"];
        string File = strFilePath + strDate;
        eGoldFunction ef = new eGoldFunction();
        ef.IsFile(File);

        Memcached.JSON c = new JSON();
        string strIP = ConfigurationManager.AppSettings["Stock_Info"];
        string strKey1 = "EMONEY_DATA_GOLD_SXLBS_GOODS_WEEK";//, "EMONEY_DATA_CCTV_SXLBB_GOODS_WEEK" };//, "EMONEY_DATA_CCTV_SXLBS_GROUP_HY_WEEK", "EMONEY_DATA_CCTV_SXLBB_GROUP_HY_WEEK" };   //EMONEY_DATA_1.0_TOP_GROUP_HY_DOWN
        string strKey2 = "EMONEY_DATA_GOLD_SXLBB_GOODS_WEEK";
        string strKey3 = "EMONEY_DATA_GOLD_SXLBS_GROUP_HY_WEEK";
        string strKey4 = "EMONEY_DATA_GOLD_SXLBB_GROUP_HY_WEEK";
        string strJson1 = c.GetJsonByKeyIP(strKey1, strIP);
        string strJson2 = c.GetJsonByKeyIP(strKey2, strIP);
        string strJson3 = c.GetJsonByKeyIP(strKey3, strIP);
        string strJson4 = c.GetJsonByKeyIP(strKey4, strIP);
        //string[] keysph = { "EMONEY_DATA_1.0_TOP_GOODS_SHA_DOWN", "EMONEY_DATA_1.0_TOP_GOODS_SHA_UP", "EMONEY_DATA_1.0_TOP_GOODS_SZA_DOWN", "EMONEY_DATA_1.0_TOP_GOODS_SZA_UP" };
        //strReturn = c.GetMultiJsonByKeysIP(keysph, strIP);

        List<eGold_TJTX> sxlbs_g;
        sxlbs_g = (List<eGold_TJTX>)(Newtonsoft.Json.JsonConvert.DeserializeObject<List<eGold_TJTX>>(strJson1));
        List<eGold_TJTX> sxlbb_g;
        sxlbb_g = (List<eGold_TJTX>)(Newtonsoft.Json.JsonConvert.DeserializeObject<List<eGold_TJTX>>(strJson2));


        Chart1.Series["Series1"]["PointWidth"] = "0.8";
        Chart1.Series["Series2"].IsVisibleInLegend = true;
        Chart1.Series["Series3"].IsVisibleInLegend = true;
        Chart1.Series["Series4"].IsVisibleInLegend = true;
        
        foreach (eGold_TJTX d in  sxlbs_g)   //sxlbb_g
        {

            string[] bsg = d.N.Split(',');
            string[] bsg_r0 = d.R0.Split(',');
            string[] bsg_r1 = d.R1.Split(',');
            for (int i = bsg.Length - 1; i >= 0; i--)
            {
                Chart1.Series["Series1"].Points.AddXY(bsg[i],0 );
                Chart1.Series["Series2"].Points.AddY(Double.Parse(bsg_r1[i]));//0
                Chart1.Series["Series3"].Points.AddY(0-Double.Parse(bsg_r0[i]));
                Chart1.Series["Series4"].Points.AddY(0 -Double.Parse(bsg_r1[i]));
            }
        }

        foreach (eGold_TJTX d in sxlbb_g)   //sxlbs_g
        {

            string[] bsg = d.N.Split(',');
            string[] bsg_r0 = d.R0.Split(',');
            string[] bsg_r1 = d.R1.Split(',');
            for (int i = bsg.Length - 1; i >= 0; i--)
            {
                Chart1.Series["Series1"].Points.AddXY(bsg[i], Double.Parse(bsg_r0[i]));   //淡红
                Chart1.Series["Series2"].Points.AddY(Double.Parse(bsg_r1[i]));    //红
                Chart1.Series["Series3"].Points.AddY(0);//0      //绿
                Chart1.Series["Series4"].Points.AddY(0 - Double.Parse(bsg_r1[i]));   //淡绿
            }
        }
     
        Chart1.Series["Series1"].ChartType = SeriesChartType.StackedBar;
        Chart1.Series["Series2"].ChartType = SeriesChartType.StackedBar;
        Chart1.Series["Series3"].ChartType = SeriesChartType.StackedBar;
        Chart1.Series["Series4"].ChartType = SeriesChartType.StackedBar;

        Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;

        Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
        Chart1.ChartAreas["ChartArea1"].AxisX.Interval =1;
        Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
        //Chart1.ChartAreas["ChartArea1"].AxisY.Title = "亿元";
       
 
        foreach (Series series in Chart1.Series)
        {
            series["StackedGroupName"] = "";
        }
        Chart1.ResetAutoValues();
       
        Chart1.ImageLocation = "data/" + strDate + "/lx_gg.png";
        Chart1.ImageStorageMode = ImageStorageMode.UseImageLocation;
        Chart1.ImageType = ChartImageType.Png; //保存格式

     //------------------------------------------------
      //------------------------------------------------
        List<eGold_TJTX> sxlbs_gp;
        sxlbs_gp = (List<eGold_TJTX>)(Newtonsoft.Json.JsonConvert.DeserializeObject<List<eGold_TJTX>>(strJson3));
        List<eGold_TJTX> sxlbb_gp;
        sxlbb_gp = (List<eGold_TJTX>)(Newtonsoft.Json.JsonConvert.DeserializeObject<List<eGold_TJTX>>(strJson4));
        foreach (eGold_TJTX d in sxlbs_gp)  //sxlbb_gp
        {

            string[] bsg = d.N.Split(',');
            string[] bsg_r0 = d.R0.Split(',');
            string[] bsg_r1 = d.R1.Split(',');
            for (int i = bsg.Length-1; i >=0; i--)
            {
                Chart2.Series["Series1"].Points.AddXY(bsg[i], 0);
                Chart2.Series["Series2"].Points.AddY((Double.Parse(bsg_r1[i])));
                Chart2.Series["Series3"].Points.AddY(0 - Double.Parse(bsg_r0[i]));
                Chart2.Series["Series4"].Points.AddY(0 - Double.Parse(bsg_r1[i]));

                //Chart2.Series["Series1"].Points.InsertXY(bsg.Length - i - 1, bsg[i], 0);//0
                //Chart2.Series["Series2"].Points.InsertY(bsg.Length - i - 1, (Double.Parse(bsg_r1[i])));//0
                //Chart2.Series["Series3"].Points.InsertY(bsg.Length - i - 1, 0 - Double.Parse(bsg_r0[i]));//0
                //Chart2.Series["Series4"].Points.InsertY(bsg.Length - i - 1, 0 - Double.Parse(bsg_r1[i]));//0
            }
        }

        foreach (eGold_TJTX d in sxlbb_gp )  //sxlbs_gp
        {

            string[] bsg = d.N.Split(',');
            string[] bsg_r0 = d.R0.Split(',');
            string[] bsg_r1 = d.R1.Split(',');
            for (int i = bsg.Length - 1; i >= 0; i--)
            {
                Chart2.Series["Series1"].Points.AddXY(bsg[i], Double.Parse(bsg_r0[i]));   //淡红
                Chart2.Series["Series2"].Points.AddY(Double.Parse(bsg_r1[i]));    //红
                Chart2.Series["Series3"].Points.AddY(0);//0      //绿
                Chart2.Series["Series4"].Points.AddY(0 - Double.Parse(bsg_r1[i]));   //淡绿
            }
        }

        Chart2.Series["Series1"].ChartType = SeriesChartType.StackedBar;
        Chart2.Series["Series2"].ChartType = SeriesChartType.StackedBar;
        Chart2.Series["Series3"].ChartType = SeriesChartType.StackedBar;
        Chart2.Series["Series4"].ChartType = SeriesChartType.StackedBar;

        Chart2.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;

        Chart2.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
        Chart2.ChartAreas["ChartArea1"].AxisX.Interval = 1;
        Chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
        foreach (Series series in Chart2.Series)
        {
            series["StackedGroupName"] = "";
        }
        Chart2.ResetAutoValues();

        Chart2.ImageLocation = "data/" + strDate + "/lx_bk.png";
        Chart2.ImageStorageMode = ImageStorageMode.UseImageLocation;
        Chart2.ImageType = ChartImageType.Png; //保存格式



      
    }

    protected void saveimg_Click(object sender, EventArgs e)
    {
        //文件转byte
        string strDate = System.DateTime.Now.Date.ToString("yyyyMMdd");
        //如果没有当天的文件夹，则生成    
        string strFilePath = ConfigurationManager.AppSettings["eGold_filepath"];
        string strIMGPath = ConfigurationManager.AppSettings["eGold_imgpath"];
        string strIMGUrlPath = ConfigurationManager.AppSettings["eGold_imgurlpath"];


        //是否存在文件夹
        eGoldFunction ef = new eGoldFunction();
        ef.IsFile(strFilePath + strDate + "/em_tjtx");
        //------------------

        string msg = "''";
        FileStream stream = new FileStream(strFilePath + strDate + "/lx_gg.png", FileMode.Open);
        byte[] bkup = new byte[stream.Length];
        stream.Read(bkup, 0, (int)stream.Length);
        //本地
        FileStream streamlocal = new FileStream(strFilePath + strDate + "/em_tjtx/lx_gg.png", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        streamlocal.Write(bkup, 0, bkup.Length);
        streamlocal.Close();


        FileStream stream1 = new FileStream(strFilePath + strDate + "/lx_bk.png", FileMode.Open);
        byte[] bkdown = new byte[stream1.Length];
        stream1.Read(bkdown, 0, (int)stream1.Length);
        //本地
        FileStream streamlocal1 = new FileStream(strFilePath + strDate + "/em_tjtx/lx_bk.png", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        streamlocal1.Write(bkdown, 0, bkdown.Length);
        streamlocal1.Close();

        stream1.Close();
        stream.Close();
        Deliver.AsynchronousClient.StartTranCommand(bkup, strIMGPath + strDate + "/lx_gg.png", out msg);
        Deliver.AsynchronousClient.StartTranCommand(bkdown, strIMGPath + strDate + "/lx_bk.png", out msg);  
    }
}